<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Act as ActModel;
use app\common\model\ActsOrder as ActsOrderModel;
use app\common\model\Recharge as RechargeModel;
use app\common\model\RechargePraise as RechargePraiseModel;

/**
 * 寺院项目控制器
 * Class Act
 * @package app\home\controller
 */
class Act extends Base
{
    /**
     * [getFindone  根据项目id获取项目]
     * @param
     * @return json
     * @author [C。]
     */
    public function getFindone()
    {
        $model = new ActModel();
        $rmodel = new RechargeModel();
        $omodel = new ActsOrderModel;
        $param = input('param.');
        try{
            $act = $model->findData($param['id'],'id','id,title,cover,amount,aptitude,feedback,content,comid');
            $http = substr($act['cover'],0,4);
            if($http != 'http'){
                $act['cover'] = config('domain_name').$act['cover'];
            }
            $count = $omodel->getRechargeCount(['aid'=>$act['id']]);
            $hasamount = $omodel->getAlreadyRaising($act['id']);

            $counts = $rmodel->getRechargeCount(['item_id'=>$act['id'],'states'=>1]);
            $hasamounts = $rmodel->getAlreadyRaising($act['id']);
            $act['content'] = percent($act['content']);
            $act['aptitude'] = percent($act['aptitude']);
            $act['feedback'] = percent($act['feedback']);
            if($hasamount + $hasamounts >= $act['amount']){
                $act['isstop'] = 1;
            }else{
                $act['isstop'] = 0;
            }
            $act['love'] = $counts + $count;
            $act['jindu'] = round(($hasamount + $hasamounts)/$act['amount'] * 100,2).'%';
            $act['amount'] = round($act['amount'],2);
            $act['hasraising'] = round($hasamount + $hasamounts,2);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/act/getFindone');
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$act);
    }
    /**
     * [getShizhu  根据项目id获取捐款的人]
     * @param
     * @return json
     * @author [C。]
     */
    public function getShizhu()
    {
        $rmodel = new RechargeModel();
//        $rpmodel = new RechargePraiseModel();
        $omodel = new ActsOrderModel;
        $param = input('param.');
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['r.item_id'] = $param['id'];
            $map['r.states'] = 1;
            $count = $rmodel->getCount($map,1);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $rmodel->getlist($map,$this->from, $this->size,1,1);
//            $mem = $this->getMember($param['token']);

            $counts = $omodel->getCount(['aid'=>$param['id']]);
            $list = $omodel->getlist(['aid'=>$param['id']],$this->from, $this->size);

            foreach($lists as $k=>$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $lists[$k]['img'] = config('domain_name').$v['img'];
                }
//                $pcount = $rpmodel->getCount(['rid'=>$v['id']]);
//                $lists[$k]['count'] = $pcount;
//                $praise = $rpmodel->getOne(['memid'=>$mem['id'],'rid'=>$v['id']]);
                $lists[$k]['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
//                if(!empty($praise)){
//                    $lists[$k]['ispraise'] = 1;
//                }else{
//                    $lists[$k]['ispraise'] = 0;
//                }
            }

            foreach($list as &$v1){
                $v1['username'] = $v1['nickname'];
                $v1['paytime'] = $v1['create_time'];
                $v1['amt'] = $v1['money'];
                $v1['rmk'] = $v1['content'];
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/act/getShizhu');
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'counts' => $counts,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
            'list' => $list
        ]);
    }
    /**
     * [getParise  点赞]
     * @param
     * @return json
     * @author [C。]
     */
    public function getParise()
    {
        $rpmodel = new RechargePraiseModel();
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        try{
            $praise = $rpmodel->getOne(['memid'=>$mem['id'],'rid'=>$param['id']]);
            if(!empty($praise)){
                return admin_json(2, '该用户已点赞');
            }
            $rpmodel->addPariseData(['memid'=>$mem['id'],'rid'=>$param['id']]);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/act/getParise');
            return admin_json(-1, '点赞失败'.$e->getMessage());
        }
        return admin_json(1, '点赞成功');
    }
    /**
     * [getAll  获取所有项目]
     * @param
     * @return json
     * @author [C。]
     */
    public function getAll()
    {
        $param = input('param.');
        $model = new ActModel;
        $omodel = new ActsOrderModel;
        $rmodel = new RechargeModel();
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['states'] = ['in','1,3'];
            $counts = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($counts / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,'id,title,cover,amount,comid,states');
            foreach($lists as &$v){
                $http = substr($v['cover'],0,4);
                if($http != 'http'){
                    $v['cover'] = config('domain_name').$v['cover'];
                }
                $count = $omodel->getRechargeCount(['aid'=>$v['id']]);
                $hasamount = $omodel->getAlreadyRaising($v['id']);

                $counts = $rmodel->getRechargeCount(['item_id'=>$v['id'],'states'=>1]);
                $hasamounts = $rmodel->getAlreadyRaising($v['id']);
                $v['love'] = $count + $counts;
                $v['jindu'] = round(($hasamount + $hasamounts)/$v['amount'] * 100,2).'%';
                $v['amount'] = round($v['amount'],2);
                $v['hasraising'] = round($hasamount + $hasamounts,2);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/act/getAll');
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $counts,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
}